using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FBGraph.Controllers.Connections.Events;
using FBGraph.Demos.AspNetMvc.Models.View.Events;
using FBGraph.Models;

namespace FBGraph.Demos.AspNetMvc.Controllers
{
    public class EventsController : BaseController
    {
        public ActionResult Index()
        {
            var events = FbContext.Events.GetFor("me");

            return View(events);
        }

        public ActionResult Rsvp(Int64 id, EventRsvpType rsvp)
        {
            var result = FbContext.Events.Rsvp(id, rsvp);

            return RedirectToAction("detail", new { id = id });
        }

        public ActionResult Attend(Int64 id)
        {
            return Rsvp(id, EventRsvpType.Attending);
        }

        public ActionResult Maybe(Int64 id)
        {
            return Rsvp(id, EventRsvpType.Maybe);
        }

        public ActionResult Decline(Int64 id)
        {
            return Rsvp(id, EventRsvpType.Declined);
        }

        public ActionResult Detail(Int64 id)
        {
            using (FbContext.StartAsync())
            {
                var evnt = FbContext.Events.Get(id);
                var feed = FbContext.Feed.GetFor(id);
                var invited = FbContext.Events.Invited.GetFor(id);
                var attending = FbContext.Events.Attending.GetFor(id);
                var maybe = FbContext.Events.Maybe.GetFor(id);
                var noreply = FbContext.Events.NoReply.GetFor(id);
                var declined = FbContext.Events.Declined.GetFor(id);

                FbContext.CompleteAsync();

                var model = new DetailData
                {
                    Event = evnt,
                    Attending = attending,
                    Declined = declined,
                    Feed = feed,
                    Invited = invited,
                    Maybe = maybe,
                    NoReply = noreply
                };

                return View(model);
            }
        }

        public ActionResult Create(String eventName)
        {
            var start = DateTime.Now.AddHours(1);
            var end = start.AddHours(2);
            var eventId = FbContext.Events.Create(null, eventName, start, end);

            CreatedIds.Add(eventId.ToString());

            return RedirectToAction("detail", new { id = eventId });
        }
    }
}